package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.Certification;
import com.qian.api.ddj.model.CertificationCondition;
import com.qian.api.ddj.model.StatusDict;
import com.qian.api.ddj.service.ICertificationService;
import com.qian.service.ddj.dao.CertificationRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjCertification;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.api.common.BaseException;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.transaction.Transactional;
import java.util.Date;

@Service
public class CertificationServiceImpl extends BaseServiceImpl<Certification, DdjCertification, Integer> implements ICertificationService {
    @Autowired
    private CertificationRepository dao;
    @Autowired
    private UserRepository userDao;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjCertification model2Entity(Certification model) {
        DdjCertification ddjCertification = null;
        if (!ObjectUtils.isEmpty(model)) {
            ddjCertification = new DdjCertification();
            ddjCertification.setId(model.getId());
            ddjCertification.setStatus(model.getCertificationCondition());
            ddjCertification.setIdCard(model.getIdCard());
            ddjCertification.setName(model.getName());
            ddjCertification.setPhone(model.getPhone());
            ddjCertification.setSubmitDate(model.getSubmitDate());
            ddjCertification.setVerifyDate(model.getVerifyDate());
            if (isNotEmpty(model.getUserId())) {
                DdjUser ddjUser = new DdjUser();
                ddjUser.setId(model.getUserId());
                ddjCertification.setUser(ddjUser);
            }

        }
        return ddjCertification;
    }

    @Override
    protected Certification entity2model(DdjCertification entity) {
        Certification certification = null;

        if (!ObjectUtils.isEmpty(entity)) {
            certification = new Certification();
            certification.setId(entity.getId());
            certification.setCertificationCondition(entity.getStatus());
            certification.setIdCard(entity.getIdCard());
            certification.setName(entity.getName());
            certification.setPhone(entity.getPhone());
            certification.setSubmitDate(entity.getSubmitDate());
            certification.setVerifyDate(entity.getVerifyDate());
            if (isNotEmpty(entity.getUser().getId())) {
                DdjUser ddjUser = new DdjUser();
                ddjUser.setId(entity.getUser().getId());
                certification.setUserId(ddjUser.getId());
            }
        }
        return certification;
    }


    @Override
    public Certification save(Certification certification) {
        DdjCertification ddjCertification = dao.findByUserId(certification.getUserId());
        DdjUser ddjUser = userDao.findOne(certification.getUserId());
        if (!isNotEmpty(ddjCertification)) {
            if (!isNotEmpty(ddjUser.getIdCard()) && !isNotEmpty(ddjUser.getName()) && !isNotEmpty(ddjUser.getPhone())) {
                certification.setCertificationCondition(StatusDict.PENDING);
                certification.setSubmitDate(new Date());
                System.out.println(certification);
                dao.save(model2Entity(certification));
            } else {
                throw new BaseException("审核已通过");
            }
        } else {
            throw new BaseException("已提交审核");

        }
        return certification;
    }

    @Override
    @Transactional
    public Certification update(Certification certification) {
        DdjCertification ddjCertification = dao.findOne(certification.getId());
        if (isNotEmpty(ddjCertification)) {
            ddjCertification.setVerifyDate(new Date());
            ddjCertification.setStatus(certification.getCertificationCondition());
            if (StatusDict.SUCCESS.equals(certification.getCertificationCondition())) {
                DdjUser ddjUser = userDao.findOne(ddjCertification.getUser().getId());
                ddjUser.setName(ddjCertification.getName());
                ddjUser.setPhone(ddjCertification.getPhone());
                ddjUser.setIdCard(ddjCertification.getIdCard());
                userDao.save(ddjUser);
            }

        } else {
            throw new BaseException("请填入状态");
        }

        certification = entity2model(dao.save(ddjCertification));
        return certification;
    }


    @Override
    public CertificationCondition QueryStatusByUserId(Integer userId) {
        DdjCertification ddjCertification = dao.findByUserId(userId);
        DdjUser ddjUser = userDao.findOne(userId);
        CertificationCondition certificationCondition = new CertificationCondition();
        if (isNotEmpty(ddjUser)) {

            certificationCondition.setIdCard(ddjUser.getIdCard());
            certificationCondition.setName(ddjUser.getName());
            certificationCondition.setPhone(ddjUser.getPhone());
            if (isNotEmpty(ddjUser.getPhone())) {
                certificationCondition.setStatus(StatusDict.SUCCESS);
            } else if (isNotEmpty(ddjCertification)) {
                certificationCondition.setStatus(StatusDict.PENDING);
            } else {
                certificationCondition.setStatus(StatusDict.FAIL);
            }
        }
        System.out.println(certificationCondition);


        return certificationCondition;
    }
}
